﻿using Biz.Core.Models;
using Biz.Core.Providers;
using Biz.Data.Entities;
using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Infrastructure;

namespace Biz.Data
{
    /// <summary>
    /// 数据提供者IDeviceDataProvider实现
    /// </summary>
    [AutoResolve]
    public class DeviceDataProvider : EntityDataProvider<SampleDbContext>, IDeviceDataProvider
    {
        private DeviceVo MapDeviceVo(DeviceInfo deviceInfo)
        {
            return new DeviceVo
            {
                DeviceId = deviceInfo.DeviceId,
                DeviceName = deviceInfo.DeviceName,
                DeviceType = deviceInfo.DeviceType,
                DisplayCode = deviceInfo.DisplayCode,
                SnCode = deviceInfo.SnCode,
                CreateTime = deviceInfo.CreateTime,
                UpdateTime = deviceInfo.UpdateTime
            };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceDto"></param>
        /// <returns></returns>
        public bool CreateDevice(DeviceDto deviceDto)
        {
            var device = new DeviceInfo
            {
                DeviceId = deviceDto.DeviceId,
                DeviceName = deviceDto.DeviceName,
                DeviceType = deviceDto.DeviceType,
                DisplayCode = deviceDto.DisplayCode,
                SnCode = deviceDto.SnCode,
                Enabled = true
            };

            return this.Insert(device) > 0;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceDto"></param>
        /// <returns></returns>
        public bool UpdateDevice(DeviceDto deviceDto)
        {
            //先查出来
            var deviceInfo = this.CurrentDbContext.DeviceInfo.SingleOrDefault(d => d.DeviceId == deviceDto.DeviceId && d.Enabled == true);

            /**
             * 以下两种为等效写法
             * 
             * 第1种
             * var deviceInfo = this.CurrentDbContext.DeviceInfo.SingleOrDefault(d => d.DeviceId == deviceDto.DeviceId && d.Enabled == true);
             * 
             * 第2种
             * var deviceInfo = (from d in this.CurrentDbContext.DeviceInfo where d.DeviceId == deviceDto.DeviceId && d.Enabled == true select d).SingleOrDefault();
             * 
             */

            //短路
            if (deviceInfo == null) return false;

            deviceInfo.DeviceName = deviceDto.DeviceName;
            deviceInfo.DeviceType = deviceDto.DeviceType;
            deviceInfo.DisplayCode = deviceDto.DisplayCode;
            deviceInfo.SnCode = deviceDto.SnCode;
            deviceInfo.UpdateTime = DateTime.Now;

            return this.Update(deviceInfo) > 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceDto"></param>
        /// <returns></returns>
        public bool DeleteDevice(string deviceId)
        {
            //先查出来
            var deviceInfo = this.CurrentDbContext.DeviceInfo.SingleOrDefault(d => d.DeviceId == deviceId);
            //短路
            if (deviceInfo == null) return false;

            return this.Delete(deviceInfo) > 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public bool ToggleDevice(string deviceId)
        {
            //先查出来
            var deviceInfo = this.CurrentDbContext.DeviceInfo.SingleOrDefault(d => d.DeviceId == deviceId);
            //短路
            if (deviceInfo == null) return false;

            deviceInfo.Enabled = !deviceInfo.Enabled;
            return this.Update(deviceInfo) > 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public DeviceVo GetDevice(string deviceId)
        {
            //先查出来
            var deviceInfo = this.CurrentDbContext.DeviceInfo.SingleOrDefault(d => d.DeviceId == deviceId);

            //短路
            if (deviceInfo == null) return null;

            //映射到VO对象上
            return MapDeviceVo(deviceInfo);
            
        }

        /// <summary>
        /// 分页查询并返回列表
        /// </summary>
        /// <param name="pagedQuery">分页查询对象，该对象主体包含分页参数，Parameters属性包含条件参数</param>
        /// <returns></returns>
        public ResultObject<IEnumerable<DeviceVo>> QueryDeviceList(PagedQueryObject<SearchModel> pagedQuery)
        {
            // 简单的单表分页查询，实体对象会通过泛型自动查找
            var result = this.PagedQueryExecute<DeviceInfo>(pagedQuery);

            /****** 以下方式常用于复杂业务场景列表分页查询
            
            1. 定义查询, 此时还未连接，未取到结果集
            var query = (from d in this.CurrentDbContext.DeviceInfo select d);
            
            2. 也可以如下以手动方式定义where子句，常用于复杂的业务，手动定义时，参数对象不可使用 TargetPropertyAttribute 特性
            var query = (from d in this.CurrentDbContext.DeviceInfo where d.DeviceName == pagedQuery.Parameters.Keyword select d);

            传入查询和分页参数以及其它条件
            var result = this.PagedQueryExecute(query, pagedQuery);


            // 注意：PagedQueryExecute 方法的内部调用过程是一个后期绑定行为，result结果在调用GetResultObject或 GetResultTableObject 之前，不会连数据库获取结果集，
                     如果需要连数据提前拿数据请调用 query.ToArray() 或 query.ToList()

            ******/


            return result.GetResultObject(d => new DeviceVo
            {
                DeviceId = d.DeviceId,
                DeviceName = d.DeviceName,
                DeviceType = d.DeviceType,
                DisplayCode = d.DisplayCode,
                SnCode = d.SnCode,
                CreateTime = d.CreateTime,
                UpdateTime = d.UpdateTime
            });
        }

    }
}
